package controller;

import aop.LogData;
import aop.LogType;
import com.github.pagehelper.PageInfo;
import entity.LayRequest;
import entity.MyResult;
import entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.RoleService;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @LogData(logType = LogType.LOGIN,description = "查询日志")
    @ResponseBody
    @RequestMapping("/laylist")
    public MyResult<List<Role>> list(LayRequest layRequest, HttpSession session){
            List<Role> roles=roleService.selectAll();
           session.setAttribute("role",roles);
            //执行分页查询
            PageInfo<Role> rolePageInfo = roleService.selectByPage(layRequest);

            //将分页查询结果传递到页面
            return new MyResult<>(0,rolePageInfo.getTotal(),rolePageInfo.getList());
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "add.jsp";
    }

    @LogData(logType = LogType.INSERT,description = "添加日志")
    @ResponseBody
    @RequestMapping("/add")
    public MyResult add(Role role){
        boolean insert = roleService.insert(role);
        return new MyResult(0);
    }
    @LogData(logType = LogType.UPDATE,description = "删除日志")
    @ResponseBody
    @RequestMapping("/delete")
    public MyResult delete(Integer id){
        boolean delete = roleService.delete(id);
        return delete?new MyResult(200,"删除成功！")
                :new MyResult(500,"删除失败！");
    }

    @RequestMapping("/toupdate")
    public String toupdate(Integer id,ModelMap modelMap){
        Role role=roleService.selectById(id);
        modelMap.addAttribute("role",role);
        return "update.jsp";
    }
    @LogData(logType = LogType.UPDATE,description = "修改日志")
    @ResponseBody
    @RequestMapping("/update")
    public MyResult update(Role role){
        boolean update = roleService.update(role);
        return new MyResult(0);
    }



}
